package com.jiutian.hotel.db.impl;

import com.jiutian.factory.HibernateSessionFactory;

import com.jiutian.hotel.db.HotelListDAO;
import com.jiutian.hotel.pojo.HotelInfoData;
import com.jiutian.hotel.pojo.HotelOrderData;
import com.jiutian.hotel.pojo.HotelRoomOfferData;
import com.jiutian.hotel.pojo.RoominfoData;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;

import java.util.ArrayList;
import java.util.List;


public class HotelListDAOImpl implements HotelListDAO {
    public List excute(String sql) {
        Session session = null;

        try {
            session = HibernateSessionFactory.getSession();

            SQLQuery sqlquery = session.createSQLQuery(sql);

            return sqlquery.list();
        } catch (Exception ex) {
            ex.printStackTrace();

            return null;
        } finally {
            session.close();
        }
    }

    public List<Object[]> excuteWithoutCache(String sql) {
        Session session = null;

        try {
            session = HibernateSessionFactory.getSession();

            // session.flush();
            SQLQuery sqlquery = session.createSQLQuery(sql);
            List result = sqlquery.list();
            session.clear();

            return result;
        } catch (Exception ex) {
            ex.printStackTrace();

            return null;
        } finally {
            session.close();
        }
    }

    public Object get1(String sql) {
        Session session = null;

        try {
            session = HibernateSessionFactory.getSession();

            SQLQuery sqlquery = session.createSQLQuery(sql);

            return sqlquery.uniqueResult();
        } catch (Exception ex) {
            ex.printStackTrace();

            return null;
        } finally {
            session.close();
        }
    }

    public boolean dealOrder(ArrayList<HotelOrderData> list,
        HotelRoomOfferData roomoffer) {
        Session session = null;
        Transaction tx = null;

        try {
            session = HibernateSessionFactory.getSession();

            tx = session.beginTransaction();

            for (HotelOrderData order : list) {
                session.save(order);
            }

            session.update(roomoffer);
            tx.commit();

            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            tx.rollback();

            return false;
        } finally {
            session.close();
        }
    }

    public boolean saveOrders(ArrayList<HotelOrderData> list) {
        Session session = null;

        try {
            session = HibernateSessionFactory.getSession();

            Transaction tx = session.beginTransaction();

            for (HotelOrderData order : list) {
                session.save(order);
            }

            tx.commit();

            return true;
        } catch (Exception ex) {
            ex.printStackTrace();

            return false;
        } finally {
            session.close();
        }
    }

    public boolean updateOrders(ArrayList<HotelOrderData> list) {
        Session session = null;

        try {
            session = HibernateSessionFactory.getSession();

            Transaction tx = session.beginTransaction();

            for (HotelOrderData order : list) {
                session.update(order);
            }

            tx.commit();

            return true;
        } catch (Exception ex) {
            ex.printStackTrace();

            return false;
        } finally {
            session.close();
        }
    }

    public boolean updateRoomoffer(HotelRoomOfferData roomoffer) {
        Session session = null;

        try {
            session = HibernateSessionFactory.getSession();
            session.update(roomoffer);
            session.flush();

            return true;
        } catch (Exception ex) {
            ex.printStackTrace();

            return false;
        } finally {
            session.close();
        }
    }

    public HotelRoomOfferData getRoomOffer(String roomid) {
        Session session = null;
        String hql = "from HotelRoomOfferData p where p.roomid= :roomid";
        HotelRoomOfferData result = null;

        try {
            session = HibernateSessionFactory.getSession();

            Query query = session.createQuery(hql);
            result = (HotelRoomOfferData) query.setString("roomid", roomid)
                                               .uniqueResult();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            session.close();
        }

        return result;
    }

    public RoominfoData getRoomInfoByRoomid(String roomid) {
        Session session = null;
        String hql = "from RoominfoData p where p.roomid= :roomid";
        RoominfoData result = null;

        try {
            session = HibernateSessionFactory.getSession();

            Query query = session.createQuery(hql);
            result = (RoominfoData) query.setString("roomid", roomid)
                                         .uniqueResult();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            session.close();
        }

        return result;
    }

    public List<HotelOrderData> getHotelOrderByHotelid(String hotelid) {
        Session session = null;
        String hql = "from HotelOrderData p where p.hotelid= :hotelid order by p.orderTime";
        List<HotelOrderData> result = null;

        try {
            session = HibernateSessionFactory.getSession();

            Query query = session.createQuery(hql);
            result = query.setString("hotelid", hotelid).list();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            session.close();
        }

        return result;
    }

    public HotelInfoData getHotelInfoByHotelid(String hotelid) {
        Session session = null;
        String hql = "from HotelInfoData p where p.hotelid= :hotelid";
        HotelInfoData result = null;

        try {
            session = HibernateSessionFactory.getSession();

            Query query = session.createQuery(hql);
            result = (HotelInfoData) query.setString("hotelid", hotelid).uniqueResult();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            session.close();
        }

        return result;
    }
}
